#include "main.pb.h"
#include <brpc/server.h>
#include <butil/logging.h>

//创建一个子类，实现rpc调用功能
class EchoServiceImpl : public example::EchoService {
    public:
    //构造函数
    EchoServiceImpl() {}
    // 析构函数
    ~EchoServiceImpl() {}
    //远程调用
    void Echo(google::protobuf::RpcController *controller, 
              const ::example::EchoRequest *request,
              ::example::EchoResponse *response,
              ::google::protobuf::Closure *done) {
            //类似智能指针防止忘记停止启动
            brpc::ClosureGuard rpc_guard(done);
            std::cout << "收到消息:" << request->message() << std::endl;
            
            std::string str = request->message() + "--这是响应！！";
            //回复消息
            response->set_message(str);
    }
};
int main()
{
    //关闭rpc默认日志输出
    logging::LoggingSettings settings;
    settings.logging_dest = logging::LoggingDestination::LOG_TO_NONE;
    logging::InitLogging(settings);
    //构建服务器对象
    brpc::Server server;
    //向echoservice 新增服务
    EchoServiceImpl echo_service;
    //添加服务
    auto ret = server.AddService(&echo_service, brpc::ServiceOwnership::SERVER_DOESNT_OWN_SERVICE);
    if(ret == -1) {
        std::cout << "添加失败" << std::endl;
        return -1;
    }
    //启动服务器
    brpc::ServerOptions options;
    ////连接空闲超时时间-超时后连接被关闭
    options.idle_timeout_sec = -1;
    // io线程数量
    options.num_threads = 1;
    ret = server.Start(8080, &options);
    if (ret == -1) {
        std::cout << "启动服务器失败！\n";
        return -1;
    }
    //修改等待运行结束
    server.RunUntilAskedToQuit();
    return 0;
}